package com.ricchieri.manage.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedProperty;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.CharterFilter;
import com.ricchieri.model.Charter;
import com.ricchieri.model.CharterType;
import com.ricchieri.service.interf.ICharterService;
import com.ricchieri.service.interf.ICharterTypeService;
import com.ricchieri.util.FacesUtil;
import com.ricchieri.util.StringUtils;

public abstract class CommonCharterManageBean extends GenericManageBean {

	private static final long serialVersionUID = 195260936475629508L;

	@ManagedProperty(value = "#{CharterTypeService}")
	ICharterTypeService charterService;
	
	@ManagedProperty(value = "#{CharterService}")
	ICharterService charterServices;
	
	protected String selectedCharted;
		
	private Map<String,String> charterTypes = null;
	
	private Charter charter;
	
	private String orginalPatent;
	private int originalCharterNumber;
	
	@PostConstruct
	public void init() {
		charter = (Charter) FacesUtil.getParametersFlash(BusinessSettings.KEY_CHARTER);
		
		if(isRequiredCharter() && charter == null) {
			FacesUtil.redirectToNavigationCase(BusinessSettings.SEARCH_CHARTER);
		} else if(charter != null) {
			originalCharterNumber = charter.getCharterNumber();
			orginalPatent = charter.getPatent();
		}
	}
	
	public Charter getCharter() {
		if(charter == null) {
			charter = new Charter();
		}
		return charter;
	}

	public void setCharter(Charter selectedCharter) {
		this.charter = selectedCharter;
	}
	
	public ICharterTypeService getCharterService() {
		return charterService;
	}

	public void setCharterService(ICharterTypeService charterService) {
		this.charterService = charterService;
	}

	public Map<String, String> getCharterTypes() {
		if (charterTypes == null){
			List<CharterType> charterT = getCharterService().getAllCharterTypes();		
			
			charterTypes = new HashMap<String, String>();
			
			for (CharterType charterType : charterT) {
				getCharterTypes().put(FacesUtil.getResourceBundle().getString(charterType.getDescription()), String.valueOf(charterType.getId()));
			}
		}
		
		return charterTypes;
	}
	
	protected CharterType obtainCharterType(String id) {
		List<CharterType> charterT = getCharterService().getAllCharterTypes();	
		for (CharterType charterType : charterT) {
			if(id.equals(String.valueOf(charterType.getId()))) {
				return charterType;
			}
		}
		return null;
	}

	public void setCharterTypes(Map<String, String> charterTypes) {
		this.charterTypes = charterTypes;
	}
	
	public abstract String save();
	public abstract Boolean isRequiredCharter();

	public abstract String getSelectedCharted();

	public void setSelectedCharted(String selectedCharted) {
		this.selectedCharted = selectedCharted;
	}

	public ICharterService getCharterServices() {
		return charterServices;
	}

	public void setCharterServices(ICharterService charterServices) {
		this.charterServices = charterServices;
	}
	
	protected Boolean validateForm() {
		boolean isValid = Boolean.TRUE;
		
		if (this.getCharter().getCharterNumber() <= 0) {
			isValid = Boolean.FALSE;
			FacesUtil.addErrorMsg("charterForm:charterNumber", "charterNumberNoNegative");
		} else if(this.getCharter().getCharterNumber() != this.originalCharterNumber){
			CharterFilter charterFilter = new CharterFilter();
			charterFilter.setCharterNumber(this.getCharter().getCharterNumber());
			charterFilter.setFindActiveInactive(Boolean.FALSE);
			int count = charterServices.getAllCharterCountByFilter(charterFilter);
			
			if(count >0) {
				isValid = Boolean.FALSE;
				FacesUtil.addErrorMsg("charterForm:charterNumber", "charterNumberExist");
			}
		}
		
		if (this.getCharter().getKilometers() < 0) {
			isValid = Boolean.FALSE;
			FacesUtil.addErrorMsg("charterForm:kilometers", "charterKilometersNoNegative");
		}
		
		if (this.getCharter().getPlaces() <= 0) {
			isValid = Boolean.FALSE;
			FacesUtil.addErrorMsg("charterForm:places", "charterPlacesNoNegative");
		}
		
		if (this.getCharter().getStart() == null) {
			isValid = Boolean.FALSE;
			FacesUtil.addErrorMsg("charterForm:inlineCal", "requiredCharterDate");
		}
		
		if (StringUtils.isEmpty(this.getCharter().getPatent())) {
			isValid = Boolean.FALSE;
			FacesUtil.addErrorMsg("charterForm:patent", "requiredPatent");

		} else if(!this.getCharter().getPatent().equals(orginalPatent)) {
			CharterFilter charterFilter = new CharterFilter();
			charterFilter.setPatent(this.getCharter().getPatent());
			charterFilter.setFindActiveInactive(Boolean.FALSE);
			int count = charterServices.getAllCharterCountByFilter(charterFilter);
			
			if(count >0) {
				isValid = Boolean.FALSE;
				FacesUtil.addErrorMsg("charterForm:patent", "charterPatentExist");
			}
		}

		return isValid;
	}

	public String getOrginalPatent() {
		return orginalPatent;
	}

	public void setOrginalPatent(String orginalPatent) {
		this.orginalPatent = orginalPatent;
	}

	public int getOriginalCharterNumber() {
		return originalCharterNumber;
	}

	public void setOriginalCharterNumber(int originalCharterNumber) {
		this.originalCharterNumber = originalCharterNumber;
	}

}
